﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using WmsApp.Exceptions;
using WmsApp.Lookup;
using WmsApp.WindowControl;
using WmsApp.WmsService;
using WmsApp.Service;

namespace WmsApp.Window
{
    public partial class FormInOutStoreType : BaseForm
    {
        public FormInOutStoreType()
        {
            InitializeComponent();
            this.splitContainerControlMain.Panel1.Width = this.splitContainerControlMain.Width/2;
            //Lookup.LookupBind.BindingLookUpEditIsAlive(this.gridView1, this.gridColumn3);
            //Lookup.LookupBind.BindingLookUpEditIsAlive(this.gridView2, this.gridColumn6);
            this.gridView1.CustomRowCellEdit += GridViewMain1CustomRowCellEdit;
            this.gridView2.CustomRowCellEdit += GridViewMain2CustomRowCellEdit;
            CmdQuery1_Click(null, null);
            CmdQuery2_Click(null, null);
        }

        void GridViewMain1CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            var inStoreType = this.gridView1.GetRow(e.RowHandle) as InStoreType;
            if (inStoreType != null)
            {
                if(inStoreType.Editable == Constant.IsEditableFalse)
                {
                    if (e.Column == this.gridColumn1 || e.Column == this.gridColumn2)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                    else if (e.Column == this.gridColumn3)
                    {
                        if (e.RepositoryItem != null)
                        {
                            e.RepositoryItem = GetRepositoryItem(true);
                        }
                    }
                }
                else
                {
                    if (e.Column == this.gridColumn1 && inStoreType.WcfEntityState != EntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                    if (e.Column == this.gridColumn3)
                    {
                        if (e.RepositoryItem != null)
                        {
                            e.RepositoryItem = GetRepositoryItem(false);
                        }
                    }
                }
             
            }
        }

        void GridViewMain2CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            var outStoreType = this.gridView2.GetRow(e.RowHandle) as OutStoreType;
            if (outStoreType != null)
            {
                if (outStoreType.Editable == Constant.IsEditableFalse)
                {
                    if (e.Column == this.gridColumn4 || e.Column == this.gridColumn5)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                    else if (e.Column == this.gridColumn6)
                    {
                        if (e.RepositoryItem != null)
                        {
                            e.RepositoryItem = GetRepositoryItem(true);
                        }
                    }
                }
                else
                {
                    if (e.Column == this.gridColumn4 && outStoreType.WcfEntityState != EntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                    if (e.Column == this.gridColumn6)
                    {
                        if (e.RepositoryItem != null)
                        {
                            e.RepositoryItem = GetRepositoryItem(false);
                        }
                    }
                }

            }
        }

        private  RepositoryItem GetRepositoryItem(bool readOnly)
        {
            var itemLookUpEdit = new RepositoryItemLookUpEdit { AutoHeight = false };

            itemLookUpEdit.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Code", "代码"),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "名称")});
            itemLookUpEdit.DisplayMember = "Name";
            itemLookUpEdit.Name = "repositoryItemLookUpEditAliveState";
            itemLookUpEdit.ValueMember = "Code";
            itemLookUpEdit.NullText = "";
            itemLookUpEdit.ReadOnly = readOnly;
            itemLookUpEdit.DataSource = AliveState.GetAliveStateList();
            return itemLookUpEdit;
        }

        public override string HeadText
        {
            get { return "[出库类型/出库类型]管理"; }
        }

        private void CmdQuery1_Click(object sender, EventArgs e)
        {
            var service = EntityServiceFactory.GetEntityService<InStoreType, InStoreTypeService>();
            this.gridControlMain1.DataSource = service.GetEntityList();
        }

        private void CmdNew1_Click(object sender, EventArgs e)
        {
            var inStoreType = InStoreType.CreateInStoreType();
            if (this.gridControlMain1.DataSource == null)
            {
                this.gridControlMain1.DataSource = new List<InStoreType>();
            }
            ((List<InStoreType>)this.gridControlMain1.DataSource).Add(inStoreType);
            this.gridControlMain1.RefreshDataSource();

            for (int idx = 0; idx < ((List<InStoreType>)this.gridControlMain1.DataSource).Count; idx++)
            {
                var u = (InStoreType)this.gridView1.GetRow(idx);

                if (u == inStoreType)
                {
                    this.gridView1.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave1_Click(object sender, EventArgs e)
        {
            try
            {
                EntityServiceFactory.GetEntityService<InStoreType, InStoreTypeService>().
                SaveEntityList(EntityControl.EntityFilterByState.GetNeedSaveEntity(this.gridControlMain1.DataSource as List<InStoreType>));
                XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            var service = EntityServiceFactory.GetEntityService<OutStoreType, OutStoreTypeService>();
            this.gridControlMain2.DataSource = service.GetEntityList();
        }

        private void CmdNew2_Click(object sender, EventArgs e)
        {
            var outStoreType = OutStoreType.CreateOutStoreType();
            if (this.gridControlMain2.DataSource == null)
            {
                this.gridControlMain2.DataSource = new List<OutStoreType>();
            }
            ((List<OutStoreType>)this.gridControlMain2.DataSource).Add(outStoreType);
            this.gridControlMain2.RefreshDataSource();

            for (int idx = 0; idx < ((List<OutStoreType>)this.gridControlMain2.DataSource).Count; idx++)
            {
                var u = (OutStoreType)this.gridView2.GetRow(idx);

                if (u == outStoreType)
                {
                    this.gridView2.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave2_Click(object sender, EventArgs e)
        {
            try
            {
                EntityServiceFactory.GetEntityService<OutStoreType, OutStoreTypeService>().
                SaveEntityList(EntityControl.EntityFilterByState.GetNeedSaveEntity(this.gridControlMain2.DataSource as List<OutStoreType>));
                XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }
    }
}